package com.hanrui.android.activity.performance;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.hanrui.android.R;
import com.hanrui.android.adapter.PerformDetailAdapter;
import com.hanrui.android.adapter.YearPerformDetailAdapter;
import com.hanrui.android.library.base.ActionBarBaseActivity;
import com.hanrui.android.model.AllYearPerformanceBean;
import com.hanrui.android.model.PerformanceDetailBean;
import com.hanrui.android.model.YearPerformanceDetailBean;
import com.hanrui.android.model.performance.FirstQuarterLeaderBean;
import com.hanrui.android.model.performance.FourthQuarterLeaderBean;
import com.hanrui.android.model.performance.SecondQuarterLeaderBean;
import com.hanrui.android.model.performance.ThirdQuarterLeaderBean;
import com.hanrui.android.model.performance.YearPlanBean;
import com.hanrui.android.net.contract.message.performance.PerformanceDetailContract;
import com.hanrui.android.net.presenter.message.perfromance.PerformanceDetailPresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PerformDetailActivity extends ActionBarBaseActivity<PerformanceDetailPresenter> implements PerformanceDetailContract.View {
    @BindView(R.id.main_title)
    TextView mainTitle;
    @BindView(R.id.subhead_title)
    TextView subheadTitle;
    @BindView(R.id.performance_recyclerview)
    RecyclerView mRecyclerView;

    @Inject
    PerformDetailAdapter adapter;

    @Inject
    YearPerformDetailAdapter yAdapter;

    String id;
    int type;

    List<AllYearPerformanceBean>  dataList;

    @Override
    protected void initInjector() {mActivityComponent.inject(this); }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_performancedetail);

        unbinder = ButterKnife.bind(this);

        mTitleBar.setTitle("绩效目标");

        id = getIntent().getStringExtra("id");
        type = getIntent().getIntExtra("type",0);

        if (type == 0) {

            mPresenter.performanceManagementDetail(this,id);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mRecyclerView.setAdapter(adapter);
            adapter.setEmptyView(getNoDataView(mRecyclerView));
            adapter.setEnableLoadMore(false);

        }else {

            mPresenter.yearPlanDetail(this,"");
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mRecyclerView.setAdapter(yAdapter);
            yAdapter.setEmptyView(getNoDataView(mRecyclerView));
            yAdapter.setEnableLoadMore(false);

        }





    }

    @Override
    public void performanceManagementDetail(PerformanceDetailBean data) {

        if (null != data) {

            mainTitle.setText(data.getUserName()+"的绩效目标");
            subheadTitle.setText(data.getCreateDate());

            adapter.setNewData(data.getPerformanceList());


        		}

    }

    @Override
    public void yearPlanDetail(YearPerformanceDetailBean data) {

        if (null != data) {

            if (null == dataList) {

                dataList = new ArrayList<>();
            }

            List<YearPerformanceDetailBean.KeyListBean>  keyListBeanList = data.getKeyList();
            List<YearPerformanceDetailBean.CommonListBean>  commonListBeanList = data.getCommonList();
            List<YearPerformanceDetailBean.TotalListBean>  totalListBeanList = data.getTotalList();

            dataList.clear();

            if (null != keyListBeanList && keyListBeanList.size() > 0) {

               for(YearPerformanceDetailBean.KeyListBean keyListBean : keyListBeanList){

                   AllYearPerformanceBean bean;
                   String id = keyListBean.getId();
                   YearPlanBean yearPlan = keyListBean.getYearPlan();
                   String type = keyListBean.getType();
                   String rate = keyListBean.getRate();
                   String allYearPlan = keyListBean.getAllYearPlan();
                   String firstQuarterPlan = keyListBean.getFirstQuarterPlan();
                   String secondQuarterPlan = keyListBean.getSecondQuarterPlan();
                   String thirdQuarterPlan = keyListBean.getThirdQuarterPlan();
                   String fourthQuarterPlan = keyListBean.getFourthQuarterPlan();
                   FirstQuarterLeaderBean firstQuarterLeaderBean = keyListBean.getFirstQuarterLeader();
                   SecondQuarterLeaderBean secondQuarterLeaderBean = keyListBean.getSecondQuarterLeader();
                   ThirdQuarterLeaderBean thirdQuarterLeaderBean = keyListBean.getThirdQuarterLeader();
                   FourthQuarterLeaderBean fourthQuarterLeaderBean = keyListBean.getFourthQuarterLeader();
                   String rules = keyListBean.getRules();
                   int score = keyListBean.getScore();

                   bean = new AllYearPerformanceBean(id,yearPlan,type,rate,
                           allYearPlan,firstQuarterPlan,secondQuarterPlan,thirdQuarterPlan,
                           fourthQuarterPlan,firstQuarterLeaderBean,secondQuarterLeaderBean,
                           thirdQuarterLeaderBean,fourthQuarterLeaderBean,rules,score);

                   dataList.add(bean);

               }

            		}


            if (null != commonListBeanList && commonListBeanList.size() > 0) {

                for(YearPerformanceDetailBean.CommonListBean commonListBean : commonListBeanList){

                    AllYearPerformanceBean bean;
                    String id = commonListBean.getId();
                    YearPlanBean yearPlan = commonListBean.getYearPlan();
                    String type = commonListBean.getType();
                    String rate = commonListBean.getRate();
                    String allYearPlan = commonListBean.getAllYearPlan();
                    String firstQuarterPlan = commonListBean.getFirstQuarterPlan();
                    String secondQuarterPlan = commonListBean.getSecondQuarterPlan();
                    String thirdQuarterPlan = commonListBean.getThirdQuarterPlan();
                    String fourthQuarterPlan = commonListBean.getFourthQuarterPlan();
                    FirstQuarterLeaderBean firstQuarterLeaderBean = commonListBean.getFirstQuarterLeader();
                    SecondQuarterLeaderBean secondQuarterLeaderBean = commonListBean.getSecondQuarterLeader();
                    ThirdQuarterLeaderBean thirdQuarterLeaderBean = commonListBean.getThirdQuarterLeader();
                    FourthQuarterLeaderBean fourthQuarterLeaderBean = commonListBean.getFourthQuarterLeader();
                    String rules = commonListBean.getRules();
                    int score = commonListBean.getScore();

                    bean = new AllYearPerformanceBean(id,yearPlan,type,rate,
                            allYearPlan,firstQuarterPlan,secondQuarterPlan,thirdQuarterPlan,
                            fourthQuarterPlan,firstQuarterLeaderBean,secondQuarterLeaderBean,
                            thirdQuarterLeaderBean,fourthQuarterLeaderBean,rules,score);

                    dataList.add(bean);

                }

            }


            if (null != totalListBeanList && totalListBeanList.size() > 0) {

                for(YearPerformanceDetailBean.TotalListBean totalListBean : totalListBeanList){

                    AllYearPerformanceBean bean;
                    String id = totalListBean.getId();
                    YearPlanBean yearPlan = totalListBean.getYearPlan();
                    String type = totalListBean.getType();
                    String rate = totalListBean.getRate();
                    String allYearPlan = totalListBean.getAllYearPlan();
                    String firstQuarterPlan = totalListBean.getFirstQuarterPlan();
                    String secondQuarterPlan = totalListBean.getSecondQuarterPlan();
                    String thirdQuarterPlan = totalListBean.getThirdQuarterPlan();
                    String fourthQuarterPlan = totalListBean.getFourthQuarterPlan();
                    FirstQuarterLeaderBean firstQuarterLeaderBean = totalListBean.getFirstQuarterLeader();
                    SecondQuarterLeaderBean secondQuarterLeaderBean = totalListBean.getSecondQuarterLeader();
                    ThirdQuarterLeaderBean thirdQuarterLeaderBean = totalListBean.getThirdQuarterLeader();
                    FourthQuarterLeaderBean fourthQuarterLeaderBean = totalListBean.getFourthQuarterLeader();
                    String rules = totalListBean.getRules();
                    int score = totalListBean.getScore();

                    bean = new AllYearPerformanceBean(id,yearPlan,type,rate,
                            allYearPlan,firstQuarterPlan,secondQuarterPlan,thirdQuarterPlan,
                            fourthQuarterPlan,firstQuarterLeaderBean,secondQuarterLeaderBean,
                            thirdQuarterLeaderBean,fourthQuarterLeaderBean,rules,score);

                    dataList.add(bean);

                }

            }

            yAdapter.setNewData(dataList);


        }


    }


    public static void start(Context context,String id,int type){
        Intent intent = new Intent(context,PerformDetailActivity.class);
        intent.putExtra("id",id);
        intent.putExtra("type",type);
        context.startActivity(intent);
    }
}
